package cn.tedu.clyback.font.service;

import cn.tedu.clyback.font.mapper.CaculateMapper;
import cn.tedu.clyback.font.methods.LicensePlateCalculator;
import cn.tedu.clyback.font.methods.QQNumberCalculator;
import cn.tedu.clyback.font.methods.WeChatAnalysis;
import cn.tedu.clyback.font.pojo.param.CaculateParam;
import cn.tedu.clyback.font.pojo.vo.CaculateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;


@Slf4j
@Service
public class CaculateServiceImpl implements CaculateService{
    @Autowired
    private CaculateMapper caculateMapper;
    @Override
    public CaculateVO selectByPhoneNumber(CaculateParam caculateParam) {
        log.debug("caculateParam:{}",caculateParam);
        String phoneNumber=caculateParam.getPhoneNumber();
        CaculateVO cac=caculateMapper.select(phoneNumber);
        log.debug("cac:{}",cac);
        if (cac!=null){
            return cac;
        }else {
            CaculateVO calCreate= create(caculateParam.getPhoneNumber());
            caculateMapper.insert(calCreate);
            return calCreate;
        }
    }
//车牌号查询

    @Override
    public CaculateVO selectByLicense(CaculateParam caculateParam) {
        log.debug("caculateParam:{}",caculateParam);
        CaculateVO cls=caculateMapper.selectLicense(caculateParam.getLicense());
        if (cls!=null){
            return cls;
        }else {
            LicensePlateCalculator licensePlateCalculator=new LicensePlateCalculator();
            CaculateVO caculateVO= licensePlateCalculator.calculate(caculateParam.getLicense());
            caculateVO.setLicense(caculateParam.getLicense());
            caculateMapper.insertLicense(caculateVO);
            return caculateVO;
        }
    }

    //qq号查询
    @Override
    public CaculateVO selectqqNumber(CaculateParam caculateParam) {
        log.debug("caculateParam:{}",caculateParam);
        CaculateVO qqresult=caculateMapper.selectQqNumber(caculateParam.getQqNumber());
        if (qqresult!=null){
            return qqresult;
        }else {
            QQNumberCalculator qqNumberCalculator=new QQNumberCalculator();
            CaculateVO caculateVO=qqNumberCalculator.calculate(caculateParam.getQqNumber());
            caculateVO.setQqNumber(caculateParam.getQqNumber());
            caculateMapper.insertQqNumber(caculateVO);
            return caculateVO;
        }
    }

    //微信号查询
    @Override
    public CaculateVO selectwechatNumber(CaculateParam caculateParam) {
        log.debug("caculateParam:{}",caculateParam);
        CaculateVO wechatVo = caculateMapper.selectWechatNumber(caculateParam.getWechatNumber());
        if (wechatVo!=null){
            return wechatVo;
        }else {
            WeChatAnalysis weChatAnalysis=new WeChatAnalysis();
            CaculateVO caculateVO=weChatAnalysis.analyzeWeChat(caculateParam.getWechatNumber());
            caculateVO.setWechatNumber(caculateParam.getWechatNumber());
            caculateMapper.insertWechatNumber(caculateVO);
            return caculateVO;
        }
    }

    public CaculateVO create(String  number) {
//        String phoneNumber= caculateParam.getPhoneNumber();
        String score=calculatePhoneNumberScore(number)+"";
        log.debug("score:{}",score);

        String elstats=determineElementStatistics(number);
        log.debug("elstats:{}",elstats);

        String element=determineFiveElement(number);
        log.debug("element:{}",element);

        String analyzes=determineGuaXiang(number);
        log.debug("analyzes:{}",analyzes);

        CaculateVO caculateVO=new CaculateVO();
        caculateVO.setPhoneNumber(number);

        caculateVO.setResult(score);

        caculateVO.setMathematics(elstats);

        caculateVO.setStats(element);

        caculateVO.setAnalyzes(analyzes);
        log.debug("返回:{}",caculateVO);
        return caculateVO;
    }

    //判断数理
    private String determineElementStatistics(String number) {
        Random random = new Random(number.hashCode());
        int score = random.nextInt(100) + 1;
        String de=String.valueOf(score);
        return de;
    }
    // 计算手机号得分
    public String calculatePhoneNumberScore(String phoneNumber) {
        int score = 0;
        for (char c : phoneNumber.toCharArray()) {
            score += Character.getNumericValue(c);
        }
        score = score % 100 + 1;
        String sc=String.valueOf(score);
        return sc;
    }
    // 判断五行
    public String determineFiveElement(String phoneNumber) {
        int[] elementCount = new int[5]; // 木, 火, 土, 金, 水
        for (char c : phoneNumber.toCharArray()) {
            int digit = Character.getNumericValue(c);
            switch (digit) {
                case 1:
                case 2:
                    elementCount[0]++;
                    break;
                case 3:
                case 4:
                    elementCount[1]++;
                    break;
                case 5:
                case 6:
                    elementCount[2]++;
                    break;
                case 7:
                case 8:
                    elementCount[3]++;
                    break;
                case 9:
                case 0:
                    elementCount[4]++;
                    break;
            }
        }
        String[] elements = {"木", "火", "土", "金", "水"};
        int maxIndex = 0;
        for (int i = 1; i < elementCount.length; i++) {
            if (elementCount[i] > elementCount[maxIndex]) {
                maxIndex = i;
            }
        }
//        String val=elements[maxIndex];
//        log.debug(val);
        return elements[maxIndex];
    }

    // 判断卦象
    public String determineGuaXiang(String phoneNumber) {
        String[] guaXiang = {
                "乾：刚健中正，为纯阳之卦，象征天，代表着进取和创造。",
                "坤：柔顺伸展，为纯阴之卦，象征地，寓意着包容与涵养。",
                "屯：象征初生，充满艰难险阻，但也蕴含着希望和生机。",
                "蒙：象征蒙昧，意味着需要启蒙和教育，以开启智慧之门。",
                "需：象征等待，提醒人们在时机未到时要耐心守正待机。",
                "讼：象征争讼，警示人们应慎重对待争议，避免过度纷争。",
                "师：象征军队，强调率众行事要遵循正道，保持纪律严明。",
                "比：象征团结，倡导人与人之间建立诚信、友好的关系。",
                "小畜：象征小有积蓄，提醒人们要以畜养德，积少成多。",
                "履：象征小心行走，教导人们要遵循礼仪和规则行事。",
                "泰：象征通泰，阴阳交感和谐，象征着顺遂和吉祥之态。",
                "否：象征闭塞，阴阳不交，代表着困境和不顺的局面。",
                "同人：象征和同，主张上下一心，齐心协力共创美好。",
                "大有：象征富有，顺天依时，意味着能够获得丰厚的成果。",
                "谦：象征谦逊，内高外低，提醒人们保持谦虚的品德。",
                "豫：象征欢乐，顺时依势，鼓励人们及时把握快乐和机遇。",
                // 其他卦象
        };
        int lastTwoDigits = Integer.parseInt(phoneNumber.substring(phoneNumber.length() - 2));
        return guaXiang[lastTwoDigits % guaXiang.length];
    }




}
